#include<iostream>
#include<vector>
#include <algorithm>
#include <iterator>

using namespace std;

const int N = 510;
int p[N];
unsigned long long dp[N][N];
unsigned int path[N][N];
vector<int> my_out;

void _add_1_and_2( int lval,int rval)
{
	if (lval == rval)return;
	int next_value = path[lval][rval];
	vector<int>::iterator it_1 = find(my_out.begin(), my_out.end(),lval);
	my_out.insert(it_1, -1);
	vector<int>::iterator it_2 = find(my_out.begin(), my_out.end(), rval);
	//if ((it_2 + 1) == my_out.end())my_out.push_back(-2);
	my_out.insert(it_2 + 1, -2);
	_add_1_and_2(lval, next_value);
	_add_1_and_2(next_value + 1, rval);

}

void solve(int n, int p[], int out[])
{
	for (int i = 1; i <= n; i++)dp[i][i] = 0;
	for (int r = 2; r <= n; r++)//第几轮
	{
		for (int i = 1; i <= n - r + 1; i++)//第一个下标
		{
			int j = i + r - 1;
			dp[i][j] = dp[i + 1][j] + p[i - 1] * p[i] * p[j];
			path[i][j] = i;
			for (int k = i + 1; k < j; k++)
			{
				unsigned long long t = dp[i][k] + dp[k + 1][j] + p[i - 1] * p[k] * p[j];
				if (t < dp[i][j])
				{
					dp[i][j] = t;
					path[i][j] = k;
				}
			}
		}
	}

	my_out.push_back(dp[1][n] % 1000000007);
	for (int i = 1; i <= n; i++)
	{
		my_out.push_back(i);
	}
	_add_1_and_2(1, n);
	for (int i = 0; i < my_out.size(); i++)
	{
		out[i] = my_out[i];
	}
}
